﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CSVToXLS
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() != DialogResult.OK) return;
            var OpenFileName =openFileDialog1.FileName;
            textBox1.Clear();
            textBox1.Update();
            textBox1.Text = String.Concat(DateTime.Now.ToString("G"), " 开始转换", openFileDialog1.FileName,Environment.NewLine);
            List<UnZipFlieInfo> UnZipFileInfoList;
            GZIP.UnZIP(OpenFileName, out UnZipFileInfoList);
            var CSVList = UnZipFileInfoList.Where(w => Helper.IsSingleCSVFlie(w.UnZipFlieName)).Select(s => s).ToList();
            CSVList.ForEach(f =>
                                {
                                    this.Invoke(new Action<String>(textBox1.AppendText),new object[]{String.Concat(DateTime.Now.ToString("G"), " 开始转换", f.FlieName, Environment.NewLine)});
                                    f.Convert();
                                });

            var ZipFlieName = String.Concat(Path.GetDirectoryName(OpenFileName), "\\", Path.GetFileNameWithoutExtension(OpenFileName), "[经转换文件].", Helper.IsSingleCSVFlie(OpenFileName) ? "xls":"zip");
            GZIP.ZIP(ZipFlieName, UnZipFileInfoList);
            textBox1.AppendText(String.Concat(DateTime.Now.ToString("G"), " 转换后文件保存在 ", ZipFlieName, Environment.NewLine));
            textBox1.AppendText(String.Concat(DateTime.Now.ToString("G"), "  转换完成", Environment.NewLine));
            MessageBox.Show("转换完成");
        }
    }
}
